home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / _TR_CRPT.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  3KB  |  65 lines

  1. /*********
  2. *
  3. *  _TR_CRPT.C
  4. *
  5. *  by Leonard Zerman and Tom Rettig    
  6. *
  7. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  8. *
  9. *  Syntax: _tr_crypt( <*string>, <*password>, <*ret> )
  10. *
  11. *  Return: <expC> of <string> encoded/decode according to <password>.
  12. *          Return string will be same length as <string>.
  13. *          Unchanged <string> if <password> is less than 3 characters.
  14. *
  15. *  Note..: All parameters are <*expC>.
  16. *
  17. *          The following encryption algorithm handles the full 
  18. *          international ASCII character set. Passnum is an 
  19. *          integer between 1 and 254 that receives a value
  20. *          returned from _tr_pnum based on the password.
  21. *          Passnum is a seed for the encryption-key variable
  22. *          that is modified in the for-loop by the position
  23. *          of the current character in reference to the total 
  24. *          string (i - len). This produces a non-repeating 
  25. *          pattern even if all the characters are the same, 
  26. *          thus hiding the length of the password key.
  27. *          The password characters are always being rotated
  28. *          in a circular manner (que). 
  29. *          ret[i] gets the bitwise XOR of the character with 
  30. *          the XOR of passnum and the current character in the 
  31. *          password que. This gives 24 bits of encryption for
  32. *          each character. 
  33. *
  34. *          This encryption/decryption program can be broken 
  35. *          by any computer-literate person with access to this
  36. *          source code or a knowledge of cryptography.
  37. *********/
  38.  
  39. #include "trlib.h"
  40.  
  41. char *_tr_crypt( instr, pwstr, ret )
  42. char *instr, *pwstr, *ret;              /* pointers to string, password, */
  43. {                                                   /* and return string */
  44.    int i, j, len, pwlen, passnum;               
  45.    char buff[1];
  46.  
  47.    len    = _tr_strlen( instr );                     /* length of string */
  48.    pwlen  = _tr_strlen( pwstr );                   /* length of password */
  49.  
  50.    passnum = (int) ((((_tr_pnum(pwstr)/997) - 1) % 254 ) + 1);   
  51.                                                        /* get seed value */
  52.    for ( i = j = 0; i < len; i++ )               /* process whole string */
  53.    {
  54.        passnum = (int) (((passnum + ( i - len )) - 1 ) % 254) + 1; 
  55.        buff[0] = (instr[i] ^ (passnum ^ pwstr[j]) );      /* XOR 3 var's */
  56.        ret[i] = (buff[0] ? buff[0] : instr[i]);   /* if NULL return char */              
  57.        j = ( j >= pwlen ? 0 : j++ );    /* password que control variable */
  58.    }
  59.    
  60.    ret[i] = NULLC;                              /* NULL terminate string */
  61.    return( ret );                          /* send back encrypted string */
  62. }
  63.  
  64. /* eof _tr_crpt */
  65.